package Backtracking;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class 子集2 {

    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    int[] nums;
    int len;

    public List<List<Integer>> subsetsWithDup(int[] nums) {
        Arrays.sort(nums);
        this.nums = nums;
        len = nums.length;
        res.add(new ArrayList<>());
        backTracking(0);
        return res;
    }

    private void backTracking(int index) {
        if (index == len) return;
        for (int i = index; i < len; i++) {
            if (i > index && nums[i] == nums[i - 1]) continue;
            path.add(nums[i]);
            res.add(new ArrayList<>(path));
            backTracking(i + 1);
            path.removeLast();
        }
    }
}
